package scct.zhangyang.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
    //    数据库连接字符串
    private static String dbDriver = "com.mysql.cj.jdbc.Driver";
    private static String dbUrl =
            "jdbc:mysql://localhost:3306/storedb?characterEncoding=UTF-8&useSSL=false";
    private static String User = "root";
    private static String Pass = "root";

    public static Connection getConnection() {
        Connection conn;
        try {
            Class.forName(dbDriver);
            conn = DriverManager.getConnection(dbUrl, User, Pass);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    public static void closeConnection(Connection conn){
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
